package com.mr.web.support;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.util.HtmlUtils;

public class PageUtil {
	// request 中定义的参数名称
	public static final String PARAM_PAGE_NUM = "pageNo";
	public static final String PARAM_ROWS_COUNT = "rowsCount";
	public static final String PARAM_ROWS_PER_PAGE = "rowsPerPage";
	protected final transient Logger log = LoggerFactory.getLogger(getClass());
	private int currentPage;// 当前页
	private int rowsCount;// 记录数
	private int startNum;// 显示的第一页码
	private int endNum;// 显示的最后
	private int pageCount;// 分页数量
	private int rowsPerPage;// 每页显示数量
	private String scriptParams;
	private String encode = "UTF-8";
	private List<PageInfo> pageInfoList;
	private String baseUrl;

	public String getEncode() {
		return encode;
	}

	public void setEncode(String encode) {
		this.encode = encode;
	}

	public List<PageInfo> getPageInfoList() {
		return pageInfoList;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public int getRowsCount() {
		return rowsCount;
	}

	public int getRowsPerPage() {
		return rowsPerPage;
	}

	public int getPageCount() {
		return pageCount;
	}

	public PageUtil(int rowsCount, int rowsPerPage, int currentPage, String url) {
		this.rowsCount = rowsCount;
		this.rowsPerPage = rowsPerPage;
		this.currentPage = currentPage;
		this.pageCount = (rowsCount - 1) / rowsPerPage + 1;
		this.startNum = Math.max(0, currentPage - 5);
		this.endNum = Math.min(startNum + 10, pageCount);
		if (StringUtils.isBlank(url))
			this.baseUrl = "";
		else
			this.baseUrl = url;
	}

	/**
	 * 获取不同分页的URI query查询串，如<br>
	 * param1=xxxx&param2=YYYYY&pageNo=0<br>
	 * param1=TTTT&param2=SSSSS&pageNo=1<br>
	 * .......
	 */
	public void initPageInfo(Map params, List paramNames) {
		pageInfoList = new ArrayList<PageInfo>();
		// 1、获取查询串
		String commonParam = "";
		String scriptParam = "{";
		if (params != null) {
			if (paramNames == null)
				paramNames = new ArrayList(params.keySet());
			for (Object paramName : paramNames) {
				if (paramName.equals("pageNo"))
					continue;
				Object values = params.get(paramName);
				if (values == null)
					continue;
				String[] tmp = null;
				if (values instanceof String[])
					tmp = (String[]) values;
				else
					tmp = new String[] { "" + values };
				for (String value : tmp) {
					if (StringUtils.isNotBlank(value)) {
						try {
							commonParam += "&" + paramName + "="
									+ URLEncoder.encode(value, encode);
							scriptParam += "'" + paramName + "':'"
									+ HtmlUtils.htmlEscape(value) + "',";
						} catch (UnsupportedEncodingException e) {
							log.warn("", e);
						}
					}
				}
			}
		}
		// 2、整理出页码链接
		if (scriptParam.length() > 1)
			this.scriptParams = scriptParam.substring(0,
					scriptParam.length() - 1)
					+ "}";
		if (this.currentPage > 0) {// 有上一页
			PageInfo pageInfo = new PageInfo();
			pageInfo.setUrl(baseUrl + "?pageNo=" + (currentPage - 1)
					+ commonParam);
			pageInfo.setPageNo("上一页");
			pageInfo.setPrePage(true);
			pageInfo.setRealPageNo(currentPage - 1);
			pageInfoList.add(pageInfo);
		}
		for (int pn = startNum; pn < endNum; pn++) {
			PageInfo pageInfo = new PageInfo();
			pageInfo.setUrl(baseUrl + "?pageNo=" + pn + commonParam);
			pageInfo.setPageNo("" + (pn + 1));// 显示的页码
			pageInfo.setRealPageNo(pn);
			if (pn == currentPage)
				pageInfo.setCurrentPage(true);
			pageInfoList.add(pageInfo);
		}
		if (this.currentPage + 1 < this.pageCount) {// 有下一页
			PageInfo pageInfo = new PageInfo();
			pageInfo.setUrl(baseUrl + "?pageNo=" + (currentPage + 1)
					+ commonParam);
			pageInfo.setPageNo("下一页");
			pageInfo.setRealPageNo(currentPage + 1);
			pageInfo.setNextPage(true);
			pageInfoList.add(pageInfo);
		}
	}

	public void initPageInfo() {
		initPageInfo(null);
	}

	public void initPageInfo(Map<String, Object> params) {
		initPageInfo(params, null);
	}

	public String getScriptParams() {
		return scriptParams;
	}

	public void setScriptParams(String scriptParams) {
		this.scriptParams = scriptParams;
	}

}
